package com.rskj.cashbook.service.iml;

import com.rskj.cashbook.dao.DictDataDao;
import com.rskj.cashbook.service.IDictDataService;
import com.rskj.cashbook.util.SnowflakeIdWorker;
import com.rskj.cashbook.vo.DictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 字典数据服务实现类
 * @author fudaopin
 */
@Service
public class DictDataService implements IDictDataService {

    @Autowired
    private DictDataDao dictDataDao;

    @Override
    public DictData getById(String id) {
        return dictDataDao.selectById(id);
    }

    @Override
    public List<DictData> getByTypeId(String typeId) {
        return dictDataDao.selectByTypeId(typeId);
    }

    @Override
    public List<DictData> getByTypeCode(String typeCode) {
        return dictDataDao.selectByTypeCode(typeCode);
    }

    @Override
    public List<DictData> getEnabledByTypeId(String typeId) {
        return dictDataDao.selectEnabledByTypeId(typeId);
    }

    @Override
    public List<DictData> getEnabledByTypeCode(String typeCode) {
        return dictDataDao.selectEnabledByTypeCode(typeCode);
    }

    @Override
    public DictData getByCode(String code) {
        return dictDataDao.selectByCode(code);
    }

    @Override
    public DictData getByValue(String value) {
        return dictDataDao.selectByValue(value);
    }

    @Override
    public int save(DictData dictData) {
        // 设置ID和创建时间
        dictData.setId(SnowflakeIdWorker.getId());
        dictData.setCreateDate(new Date());
        dictData.setUpdateDate(new Date());
        
        // 设置默认值
        if (dictData.getEnabled() == null) {
            dictData.setEnabled(1);
        }
        if (dictData.getSort() == null) {
            dictData.setSort(0);
        }
        
        return dictDataDao.insert(dictData);
    }

    @Override
    public int update(DictData dictData) {
        // 设置更新时间
        dictData.setUpdateDate(new Date());
        return dictDataDao.update(dictData);
    }

    @Override
    public int delete(String id) {
        return dictDataDao.deleteById(id);
    }

    @Override
    public int deleteByTypeId(String typeId) {
        return dictDataDao.deleteByTypeId(typeId);
    }

    @Override
    public boolean isCodeExists(String code) {
        return dictDataDao.countByCode(code) > 0;
    }

    @Override
    public boolean isCodeExistsByTypeId(String typeId, String code) {
        return dictDataDao.countByTypeIdAndCode(typeId, code) > 0;
    }
}
